import copy

from novel.dao import POOL


def findAll():
    """
    查询所有的数据
    :return:
    """
    conn = POOL.connection()
    cursor = conn.cursor()
    type_sql = """
                           SELECT * FROM `bookdb`.`category`
                      """
    cursor.execute(type_sql)
    type_rows =cursor.fetchall()
    cursor.close()
    conn.close()
    return  type_rows


def insertCategory(name):
    """
    插入新类型
    :return:
    """
    insert_sql = """
                    INSERT INTO `bookdb`.`category`(`category_name`, `category_son_id`) VALUES (%s, '0')
                """
    conn = POOL.connection()
    cursor = conn.cursor()

    cursor.execute(insert_sql, (name))
    category_id = cursor.lastrowid
    conn.commit()
    id = copy.deepcopy(category_id)
    cursor.close()
    conn.close()
    return id


def insertNovelAndCategory(nid,cid):
    """
    添加小说和类型的中间表
    :return:
    """
    type_sql = """
                           INSERT INTO `bookdb`.`novel_and_category`(`nc_novel_id`, `nc_category_id`) VALUES (%s,%s)
                      """

    conn = POOL.connection()
    cursor = conn.cursor()
    cursor.execute(type_sql, (nid,cid))
    conn.commit()
    cursor.close()
    conn.close()
